rm(list=ls()) #clear

library(summarytools)
library(expss)
library(dplyr)
library(psych)

std01<-function(x){
  min.x<-min(x, na.rm=T)
  max.x<-max(x-min.x, na.rm=T)
  return((x-min.x)/max.x)
} 
#recode vars 0 to 1

dat<- read.csv("Study 4 - Bovitz/Wave 2/Bovitz_Qual_S4W2.csv")
load("Study 4 - Bovitz/Wave 2/demoexport.RData")

dat<-merge(dat, demoexport, by="PID", all = "TRUE")

###removing disqualified and Rs from other group###

dat<- subset(dat, dat$PID!="")
dat<- subset(dat, dat$Finished==1)
dat<- subset(dat, dat$Consent==1)
dat<- subset(dat, dat$atnchk1=="1,2")
dat<- dat[!(duplicated(dat$PID) | duplicated(dat$PID, fromLast = TRUE)), ]

dat$pid7<- ifelse(dat$pidcat==1 & dat$demstr==1, 1,
                  ifelse(dat$pidcat==1 & dat$demstr==2, 2,
                         ifelse(dat$pidcat==2 & dat$demstr==1, 7,
                                ifelse(dat$pidcat==2 & dat$demstr==2, 6, dat$lean))))
dat$pid0to1<- std01(dat$pid7)
freq(dat$pid7)

################################### DEMOGRAPHIC VARS ##################################
dat$white<- ifelse(dat$ethnicity==1, 1, 0)
dat$black<- ifelse(dat$ethnicity==2, 1, 0)
dat$asian<- ifelse(dat$ethnicity==3, 1, 0)
dat$male<- ifelse(dat$gender==1, 1, 0)
dat$hisplat<- ifelse(dat$hispanic==1, 1, 0)
dat$inc<- dat$income
dat$ba<- ifelse(dat$education > 5, 1, 0)

freq(dat$white)
freq(dat$black)
freq(dat$asian)
freq(dat$male)
freq(dat$hisplat)
freq(dat$ba)
mean(dat$age, na.rm = T)

dat<- subset(dat, dat$pid7 != 4)
#everyone but pure independents
dat$pid01<- ifelse(dat$pid7<4, 0, 1)
freq(dat$pid01)
#0 = Dem and 1 = Rep
#no ind in this dataframe so no need to define them
dat<- subset(dat, dat$atnchk2==5)
#2nd attention check that was halfway through the survey
################################### normal vars #######################################

#aggregate aps and subscale items for both 20 and 9 versions
dat$otot20<-rowMeans(with(dat, cbind(o1, o2, o3, o4, o5, o6, o7, o8)))
dat$atot20<-rowMeans(with(dat, cbind(a1, a2, a3, a4, a5, a6)))
dat$mtot20<-rowMeans(with(dat, cbind(m1, m2, m3, m4, m5, m6)))
dat$apstot20<-rowMeans(with(dat, cbind(o1, o2, o3, o4, o5, o6, o7, o8,
                                                a1, a2, a3, a4, a5, a6,
                                                m1, m2, m3, m4, m5, m6)))

dat$otot9<-rowMeans(with(dat, cbind(o1, o6, o8)))
dat$atot9<-rowMeans(with(dat, cbind(a1, a2, a4)))
dat$mtot9<-rowMeans(with(dat, cbind(m2, m4, m6)))
dat$apstot9<-rowMeans(with(dat, cbind(o1, o6, o8,
                                               a1, a2, a4, 
                                               m2, m4, m6)))

#recode all measures to 0-1
dat<- dat %>%
  mutate_at(c("otot20", "atot20", "mtot20", "apstot20",
              "otot9", "atot9", "mtot9", "apstot9"),
            funs(std01(.)))

save(dat, file = "Study 4 - Bovitz/Wave 2/dataS4W2.RData")

datT2<- dat[c("Finished", "o1", "o2", "o3", "o4", "o5", "o6", "o7", "o8",
                   "a1", "a2", "a3", "a4", "a5", "a6",
                   "m1", "m2", "m3", "m4", "m5", "m6",
                   "otot20", "atot20", "mtot20", "apstot20",
                   "otot9", "atot9", "mtot9", "apstot9",
                   "pid7", "pid01", "PID")]
save(datT2, file = "Study 4 - Bovitz/TS/dataT2.RData")

